#region The MIT License
// The MIT License
// 
// Copyright (c) 2009 octalforty studios
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#endregion
using System;

using NUnit.Framework;

using octalforty.Componento.ComponentModel.Installation;
using octalforty.Componento.ComponentModel.Tracing;
using octalforty.Componento.Shared.SemanticModel;
using Rhino.Mocks;

namespace octalforty.Componento.Tests.Componento.ComponentModel.Installation
{
    [TestFixture()]
    public class ComponentManifestProviderServiceTestFixture
    {
        [Test()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void ResolveComponentManifestProviderThrowsArgumentNullExceptionOnNullUri()
        {
            IComponentManifestProviderService componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService());
            componentManifestProvider.ResolveComponentManifestProvider(null);
        }

        [Test()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetComponentManifestThrowsArgumentNullExceptionOnNullName()
        {
            IComponentManifestProviderService componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService());
            componentManifestProvider.GetComponentManifest(null, 
                new Uri("http://foo"),
                VersionSpecification.Equal(new Version(1, 0)));
        }

        [Test()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetComponentManifestThrowsArgumentNullExceptionOnNullUri()
        {
            IComponentManifestProviderService componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService());
            componentManifestProvider.GetComponentManifest("foo",
                null, 
                VersionSpecification.Equal(new Version(1, 0)));
        }

        [Test()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void GetComponentManifestThrowsArgumentNullExceptionOnNullVersionRequirement()
        {
            IComponentManifestProviderService componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService());
            componentManifestProvider.GetComponentManifest("foo", new Uri("http://foo"), null);
        }

        [Test()]
        [ExpectedException(typeof(ComponentManifestException))]
        public void GetComponentManifestWithoutRegisteredManifestProvidersThrowsComponentInstallationException()
        {
            var componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService());
            Assert.IsNull(componentManifestProvider.GetComponentManifest("foo",
                new Uri("http://foo"), 
                VersionSpecification.Equal(new Version(1, 0))));
        }

        [Test()]
        [ExpectedException(typeof(ComponentManifestException))]
        public void GetComponentManifestThrowsComponentInstallationExceptionWhenCannotInstallMetamanifest()
        {
            var mockRepository = new MockRepository();
            var httpProvider = mockRepository.StrictMock<IComponentManifestProvider>();
            var ftpProvider = mockRepository.StrictMock<IComponentManifestProvider>();

            var rootUri = new Uri("http://foo");
            var requirement = 
                VersionSpecification.Equal(new Version(1, 0));

            using(mockRepository.Record())
            {
                Expect.Call(httpProvider.CanProvideManifestFrom(rootUri)).
                    Return(true);

                Expect.Call(httpProvider.GetComponentManifest("foo", rootUri, requirement)).
                    Return(new ComponentManifest("foo", new Version(1, 0), true, new Resource(new Uri("ftp://foo"))));

                Expect.Call(httpProvider.CanProvideManifestFrom(new Uri("ftp://foo"))).
                    Return(false);

                Expect.Call(ftpProvider.CanProvideManifestFrom(new Uri("ftp://foo"))).
                    Return(true);

                Expect.Call(ftpProvider.GetComponentManifest("foo", new Uri("ftp://foo"), requirement)).
                    Return(new ComponentManifest("foo", new Version(1, 0), true, new Resource(new Uri("svn://foo"))));

                Expect.Call(httpProvider.CanProvideManifestFrom(new Uri("svn://foo"))).
                    Return(false);

                Expect.Call(ftpProvider.CanProvideManifestFrom(new Uri("svn://foo"))).
                    Return(false);
            } // using

            var componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService())
                {
                    ManifestProviders = new[]
                        {
                            new Lazy<IComponentManifestProvider>(() => httpProvider),
                            new Lazy<IComponentManifestProvider>(() => ftpProvider)
                        }
                };

            componentManifestProvider.GetComponentManifest("foo", rootUri, requirement);
        }

        [Test()]
        public void ResolveComponentManifestProvider()
        {
            var mockRepository = new MockRepository();
            var httpProvider = mockRepository.StrictMock<IComponentManifestProvider>();
            var ftpProvider = mockRepository.StrictMock<IComponentManifestProvider>();

            var uri = new Uri("ftp://foo");

            using(mockRepository.Record())
            {
                Expect.Call(httpProvider.CanProvideManifestFrom(uri)).
                    Return(false);
                Expect.Call(ftpProvider.CanProvideManifestFrom(uri)).
                    Return(true);
            } // using

            var componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService())
                {
                    ManifestProviders = new[]
                        {
                            new Lazy<IComponentManifestProvider>(() => httpProvider),
                            new Lazy<IComponentManifestProvider>(() => ftpProvider)
                        }
                };

            using(mockRepository.Playback())
                Assert.AreSame(ftpProvider, componentManifestProvider.ResolveComponentManifestProvider(uri));
        }

        [Test()]
        public void GetComponentManifest()
        {
            var mockRepository = new MockRepository();
            var httpProvider = mockRepository.StrictMock<IComponentManifestProvider>();
            var ftpProvider = mockRepository.StrictMock<IComponentManifestProvider>();

            var uri = new Uri("ftp://foo");
            
            var fooManifest = new ComponentManifest("foo", new Version(1, 0));

            using(mockRepository.Record())
            {
                Expect.Call(httpProvider.CanProvideManifestFrom(uri)).
                    Return(false);
                Expect.Call(ftpProvider.CanProvideManifestFrom(uri)).
                    Return(true);
                Expect.Call(ftpProvider.GetComponentManifest("foo", uri, 
                    VersionSpecification.Equal(new Version(1, 0)))).
                    Return(fooManifest);
            } // using

            var componentManifestProvider = new ComponentManifestProviderService(new TraceManagerService())
                {
                    ManifestProviders = new[]
                        {
                            new Lazy<IComponentManifestProvider>(() => httpProvider),
                            new Lazy<IComponentManifestProvider>(() => ftpProvider)
                        }
                };

            using(mockRepository.Playback())
                Assert.AreSame(fooManifest, componentManifestProvider.GetComponentManifest("foo", uri, 
                    VersionSpecification.Equal(new Version(1, 0))));
        }
    }
}
